package com.jie.eduservice.controller;


import com.jie.commonutils.ResultEntity;
import com.jie.eduservice.entity.EduChapter;
import com.jie.eduservice.entity.chapter.ChapterVO;
import com.jie.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xzj
 * @since 2020-10-09
 */
//@CrossOrigin  // 解决跨域问题
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    private final EduChapterService chapterService;

    @Autowired
    public EduChapterController(EduChapterService chapterService) {
        this.chapterService = chapterService;
    }

    // 返回课程大纲列表, 根据课程id进行查询
    @GetMapping("/getChapterVideo/{courseId}")
    public ResultEntity getChapterVideo(
            @PathVariable("courseId") String courseId
    ) {
        // 某一个课程有多个章节, 一个章节有多个大纲
        List<ChapterVO> list = chapterService.getChapterVideoByCourseId(courseId);
        return ResultEntity.ok().data("allChapterVideo", list);
    }

    // 添加章节
    @PostMapping("/addChapter")
    public ResultEntity addChapter(
            // 添加的章节信息
            @RequestBody EduChapter eduChapter
    ) {
        chapterService.save(eduChapter);
        return ResultEntity.ok();
    }

    // 根据章节id查询
    @GetMapping("/getChapterInfo/{chapterId}")
    public ResultEntity getChapterInfo(
            @PathVariable("chapterId") String chapterId
    ) {
        EduChapter eduChapter = chapterService.getById(chapterId);
        return ResultEntity.ok().data("chapter", eduChapter);
    }

    // 修改章节
    @PostMapping("/updateChapter")
    public ResultEntity updateById(
            @RequestBody EduChapter eduChapter
    ) {
        chapterService.updateById(eduChapter);
        return ResultEntity.ok();
    }

    // 删除章节
    @DeleteMapping("{chapterId}")
    public ResultEntity deleteChapter(
            @PathVariable("chapterId") String chapterId
    ) {
        if (chapterService.deleteChapter(chapterId)) {
            return ResultEntity.ok();
        }
        return ResultEntity.error();
    }

}

